//
//  DataShoppingViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 lushan. All rights reserved.
//

#import "DataShoppingViewController.h"
#import "DataShoppingCell.h"
#import "DataHeadShoppingCell.h"
#import "ShoppingCollectionReusableView.h"
#import "LSAppTools.h"
#import "DataViewController.h"
#import "ShoppingDataViewController.h"
#import "MJRefresh.h"

@interface DataShoppingViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

@implementation DataShoppingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showCollectionView];

}

-(void)showCollectionView{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60 * WID, 30 * HEI)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:self.tittleName];
    customLab.font = [UIFont boldSystemFontOfSize:20];
    customLab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = customLab;
    
    self.bigArray = [NSMutableArray array];
    self.allArray = [NSMutableArray array];
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置滚动方向
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 50 * HEI) collectionViewLayout:self.flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    // 注册cell
    [self.collectionView registerClass:[DataShoppingCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[DataHeadShoppingCell class] forCellWithReuseIdentifier:@"headCell"];
    
    // 注册表头
    [self.collectionView registerClass:[ShoppingCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
    NSDictionary *headDic = @{@"key":@"587e19c223fba9a222978c2d982386dc",  @"t":@"1447900715220", @"id": self.dataId, @"version":@"2.0.08"};
    [LSAppTools getWithUrlString:@"http://api.xiangqu.com/category/out/children" body:headDic block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            HeadShoppingModel *model = [[HeadShoppingModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
        }
        [self.collectionView reloadData];
    }];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-xiangzuo.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarActionh:)];
    
    

    
    self.isUpLoading = NO;
    [self addHeader];
    [self addFooter];
    
    
}

// 设置表头方法
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    ShoppingCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head" forIndexPath:indexPath];
    
    
    return headView;
}


#pragma mark --下拉刷新
- (void)addHeader
{
    
    __block DataShoppingViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    
    // 添加下拉刷新头部控件
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        vc.nextPage = 0;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:vc.nextPage];//重新请求数据
        
    }];
    
    [self.collectionView.header beginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    __unsafe_unretained DataShoppingViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

        vc.nextPage ++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonList:vc.nextPage];//请求数据
        
    }];
}

#pragma mark --使用AFN进行数据请求
-(void)GetPersonList:(NSInteger)nextCursor{
    
    NSString *str = [NSString stringWithFormat:@"%ld", self.nextPage];
    NSDictionary *bodyDic = @{@"key":@"403ca8914ea8799f648ae0eae3ce3465", @"outCategoryId":self.dataId, @"page":str,  @"t":@"1447900715341", @"tagid":@"0", @"version":@"2.0.08"};
    [LSAppTools getWithUrlString:@"http://api.xiangqu.com/search/list" body:bodyDic block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableDictionary *dataDic = [dic objectForKey:@"data"];
        NSMutableArray *array = [dataDic objectForKey:@"records"];
        
        if (self.isUpLoading == NO) {
            [self.allArray removeAllObjects];
        }
        
        for (NSMutableDictionary *dic in array) {
            
            ShoppingModel *model = [[ShoppingModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:model];
            
        }
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
        [self.collectionView reloadData];
        
    }];
    
}

-(void)leftBarActionh: (UIBarButtonItem *)leftBar{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        self.flowLayout.itemSize = CGSizeMake(70 * WID, 80 * HEI);
        self.flowLayout.minimumLineSpacing = 10 * HEI;
        // 设置最小列间距
        self.flowLayout.minimumInteritemSpacing = 15 * WID;
        // 设置item与四周边界的距离  上左下右
        self.flowLayout.sectionInset = UIEdgeInsetsMake(10 * HEI, 20 * WID, 10 * HEI, 20 * WID);
        if (self.isUpLoading == NO) {
            self.flowLayout.headerReferenceSize = CGSizeMake(0, 0);
        }
        
    }
    else{
        self.flowLayout.itemSize = CGSizeMake(170 * WID, 230 * HEI);
        self.flowLayout.minimumLineSpacing = 8 * HEI;
        // 设置最小列间距
        self.flowLayout.minimumInteritemSpacing = 10 * WID;
        // 设置item与四周边界的距离  上左下右
        self.flowLayout.sectionInset = UIEdgeInsetsMake(10 * HEI, 10 * WID, 10 * HEI, 10 * WID);
        // 设置表头大小
        self.flowLayout.headerReferenceSize = CGSizeMake(WIDTH, 20 * HEI);
        
    }
    
    return self.flowLayout.itemSize;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return self.bigArray.count;
    }
    else{
        return self.allArray.count;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        static NSString *heaadStr = @"headCell";
        DataHeadShoppingCell  *headCell = [collectionView dequeueReusableCellWithReuseIdentifier:heaadStr forIndexPath:indexPath];
        HeadShoppingModel *model = [self.bigArray objectAtIndex:indexPath.item];
        headCell.model = model;
        return headCell;
        
    }
    else{
        static NSString *str = @"cell";
        DataShoppingCell  *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
        ShoppingModel  *model = [self.allArray objectAtIndex:indexPath.item];
        cell.model =model;
        return cell;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        ShoppingDataViewController *shoppingDVC = [[ShoppingDataViewController alloc] init];
        shoppingDVC.hidesBottomBarWhenPushed = YES;
        HeadShoppingModel *model = [self.bigArray objectAtIndex:indexPath.item];
        shoppingDVC.dataId = model.dataId;
        shoppingDVC.tittleName = model.name;
        [self.navigationController pushViewController:shoppingDVC animated:YES];
        
    }
    else if (indexPath.section == 1) {
        
        DataViewController *dataVC = [[DataViewController alloc] init];
        ShoppingModel *model = [self.allArray objectAtIndex:indexPath.item];
        dataVC.hidesBottomBarWhenPushed = YES;
        dataVC.productId = model.productId;
        [self presentViewController:dataVC animated:YES completion:^{
            
        }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
